/***************************************************
> Copyright (C) 2024 ==ASD== All rights reserved.
> File Name: hw1.c
> Author: asd
> Mail:3319941451@qq.com 
> Created Time: 2024年11月27日 星期三 11时39分34秒
***************************************************/
#include "user.h"

void displayMenu()
{
	int choice = 0;
	while (1) {
	printf("\n===ID卡登陆注册界面===\n");
	printf("1.注册\n");
	printf("2.登陆\n");
	printf("3.修改密码\n");
	printf("4.删除\n");
	printf("5.退出\n");
        printf("请选择：");
        scanf("%d", &choice);
	USER*head = NULL; 
        switch (choice) {
            case 1:
		printf("输入你想创建账户的个数\n");
		int n = 0;
		scanf("%d",&n);
                head = creatUser(head,n);
                break;
            case 2: 
               head = seekUSER(head,2);
                break;
            case 3:
              head = insertNewuser(head);
                break;
            case 4:
                head = deleteUser(head);
                break;
            case 5:
                freeLink(head);
                exit(0);
            default:
                printf("无效的选项。\n");
        }

}

// 打开日志文件，如果失败则输出错误信息并返回 NULL
FILE *openLogFile() {
    FILE *logFile = fopen(LOG_FILE_PATH, "a");
    if (logFile == NULL) {
        perror("打开日志文件失败");
    }
    return logFile;
}

// 获取当前时间并格式化为字符串形式
void getCurrentTime() 
{
    time_t currentTime;
    time(&currentTime);
    struct tm* timeInfo = localtime(&currentTime);
    char timeStr[20];
    strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", timeInfo);
    fprintf(openLogFile(), "%s ", timeStr);
}

// 记录操作和相关用户名到日志文件
void logOperation(LogLevel level, const char* operation, const char* username) 
{
    FILE* logFile = openLogFile();
    if (logFile!= NULL) {
        getCurrentTime();
        switch (level) {
        case LOG_ERROR:
            fprintf(logFile, "[ERROR] ");
            break;
        case LOG_WARNING:
            fprintf(logFile, "[WARNING] ");
            break;
        case LOG_INFO:
            fprintf(logFile, "[INFO] ");
            break;
        case LOG_DEBUG:
            fprintf(logFile, "[DEBUG] ");
            break;
        }
        fprintf(logFile, "%s - %s\n", operation, username);
        fclose(logFile);
    } else {
        // 处理日志文件打开失败的情况，可以选择输出错误信息
        perror("打开日志文件失败，无法记录操作");
    }

USER *creatUser(USER*head,int n)
{//注册
	head = (USER*)malloc(sizeof(USER));
	memset(head,0,sizeof(USER));
	USER *user =NULL;
	USER *end = head;
	int i = 0;
	for(i = 0;i < n;i++)
	{
		user = (USER*)malloc(sizeof(USER));
		memset(user,0,sizeof(USER));

		printf("input id:\n");	
		scanf("%s",user->id);
		printf("input name:\n");
		scanf("%s",user->name);
		printf("input password:\n");	
		scanf("%s",user->password);

		end->next = user;
		user->next = NULL;
		end = end->next;
	
	}
	saveUserToFile();
	return head;


//登录
USER *seekUSER(USER*head,int n)
{	
	USER*user = NULL;
	USER*newuser = NULL;
	USER*temp = NULL;
	
	char youid[20] = {0};
	char youname[20] = {0};
	char youpassword[20] = {0};

	printf("input id:\n");	
	scanf("%s",youid);
	printf("input name\n");
	scanf("%s",youname);
	printf("input password:\n");	
	scanf("%s",youpassword);
				
	user = head;
	while(user !=NULL)
	{
		if(!strcmp(user->id,youid)){
			if(!strcmp(user->password,youpassword))
			{
				printf("密码输入正确\n");
				printf("欢迎进入ID卡智能界面\n");
				displayMenu();
				break;
			} else
			{
				printf("密码输入错误\n");

				char newpassword[20] = {0};
				printf("输入新密码\n");
				scanf("%s",newpassword);

				strcpy(user->password,newpassword); 
				printf("欢迎进入ID卡智能界面\n");
				displayMenu();
				break;
			}
		}
		user = user->next;
	}
	if(user == NULL)
	{
		printf("输入错误\n");
		if(n >0)
		{
		seekUSER(head,n-1);
		}
	}
	if(n == 0)
	{
		printf("是否要重新注册\n");
		printf("1--是，0--否\n");
		int num = 0;
		scanf("%d",&num);
		switch(num)
		{
			case 1:
				insertNewuser(head);
				break;
			case 0:
				printf("下次再来吧\n");
				break;
			deafulte :
				printf("输错了\n");
				break;
		}
	}
	saveUserToFile();
	return head;
}
//重新注册
USER *insertNewuser(USER*head)
{	
	USER*user = NULL;
	USER*newuser = NULL;
	USER*temp = NULL;
	newuser = (USER*)malloc(sizeof(USER));
	memset(newuser,0,sizeof(USER));
	printf("input new id :\n");
	scanf("%s",newuser->id);
	printf("input new password :\n");
	scanf("%s",newuser->password);
	user = head;
	while(user->next !=NULL)
	{
		user = user->next;
	}
		user->next = newuser;
		newuser->next = NULL;
		printf("欢迎进入ID智能界面\n");
		displayMenu();
		saveUserToFile();
	
	return head;
}

//删除
USER* deleteUserById(USER*head)
{
    char id[20];
    printf("请输入要删除的用户 ID：");
    scanf("%s", id);
    USER* prev = NULL;
    USER* user = head;

    while (user!= NULL) 
	{
        if (!strcmp(user->id,id)) 
		{
            if (prev == NULL) 
			{
                head = user->next;
            } else 
			{
                prev->next = user->next;
            }
            free(user);
	    printf("删除用户成功\n");
            break;
        }
        prev = user;
        user = user->next;
    }

    }
    user = head->next;
    while (user!= NULL) 
{
        USER* temp = user;
        user = user->next;
        free(temp);
	saveUserToFile();
	
	return head;
    }
}
//保存用户信息到文件当中
void saveUserToFile(USER*head){
	//用来打开文件流的，它返回一个指向 FILE 结构体的指针
	FILE*fp = fopen("user_txt","w");
	 //"w" 是打开文件的模式，其中 "w" 表示以写入模式打开文件。如果文件不存在，则创建一个新文件；如果文件已存在，则将其内容清空。

	if(fp == NULL)
	{
		printf("无法打开文件。\n");
		return;
	}
	USER*user = head;
	//USER*user = NULL;
	//user = head;
	while(user !=NULL)
	{
		fprintf(fp,"%s%s%s%.2f%d",user->id,user->name,user->password,&user->thing.money,&user->thing.integral);
		 //用于将格式化的数据写入文件

		user = user->next;
	}

	fclose(fp);

	printf("已经将用户写入文件中\n");
}
//将用户信息从文件中加载
void loadUserFile(USER*head)
{
	//“r ”读模式
	FILE*fp = fopen("user.txt","r");
	if(fp == NULL)
	{
		printf("无法打开文件。\n");
		return;	
	}
	USER*user = head;
	 char id[10];
	char name[10];
	char password[20];
	float money;
	int integral;
	char activity[20];

    	while (fscanf(fp,"%s%s%s%.2f%d",user->id,user->name,user->password,&user->thing.money,&user->thing.integral)!= EOF) 
	{
        USER* newuser = (USER*)malloc(sizeof(USER));
		memset(newuser,0,sizeof(USER));
        strcpy(newuser->id, id);
        strcpy(newuser->name, name);
        strcpy(newuser->password, password);
        newuser->thing.integral = integral;
		newuser->thing.money = money;
        strcpy(newuser->thing.activity, activity);
        newuser->next = NULL;
 
        if (head == NULL) 
		{
            head = newuser;
        } else 
		{
            USER* user = head;
            while (user->next != NULL) 
			{
                user = user->next;
            }
            user->next = newuser;
        }
    }
 
    fclose(fp);
    printf("用户信息已从文件加载。\n");
}

}

//释放链表
void freeLink(USER *head)
{
	USER*temp = NULL;
	while(head->next !=NULL)
	{
		temp = head->next;
		free(head);
		head = head->next;
	}
}
